﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;
using System.Data;

public partial class MerchantRegistration : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindData();
    }

    #region store object in sessoin
    protected void OnNext(object sender, EventArgs e)
    {
        Merchant merchant = new Merchant();
        try
        {
            Address address = new Address();
            merchant = new Merchant(_dataObject);
            address = new Address(_dataObject);
            BuildObject(merchant, address);
            merchant.GetMerchantByEmailId();
            if (merchant.Id > 0)
            {
                divMessage.InnerHtml = "<strong>Error!</strong> EmailId is already Exists.";
                divMessage.Attributes.Add("class", "alert alert-danger");
                return;
            }
            merchant.CreatedDate = System.DateTime.Now;
            merchant.IsActive = true;
            merchant.IpAdded = HttpContext.Current.Request.UserHostAddress;
            merchant.Password =GetRandomStringUsingGUID(6);
            merchant.Addresses.Add(address);
            SessionWrapper.Merchant = merchant;
          //  Session["Address"] = address;
            Response.Redirect("~/MerchantSubscription.aspx");
        }
        catch (Exception ex)
        {
        }
    }
    public string GetRandomStringUsingGUID(int length)
    {
        string guidResult = System.Guid.NewGuid().ToString();
        guidResult = guidResult.Replace("-", string.Empty);
        if (length <= 0 || length > guidResult.Length)
            throw new ArgumentException("Length must be between 1 and " + guidResult.Length);
        return guidResult.Substring(0, length);
    }
    private void BuildObject(Merchant merchant, Address address)
    {
        merchant.Name = txtFirstName.Text;
        merchant.CategoryId = Convert.ToInt32(drpCategory.SelectedValue);
        merchant.Email = txtEmailId.Text;
        merchant.BusinessName = txtBusinessName.Text;
        merchant.Mobile = txtMobileNo.Text;
        merchant.IsAdmin = false;
        address.Address1 = txtAddress1.Text;
        address.Address2 = txtAddress2.Text;
        address.MerchantId = merchant.Id;
        address.Pincode = txtPostalCode.Text;
        address.CountryId = (Int32)EnumClass.Enums.Country.India;
        address.StateId = Convert.ToInt32(drpState.SelectedValue);
        //address.CityId = 1;
        address.CityId = Convert.ToInt32(lblCityId.Text);
        address.CreatedDate = DateTime.Now;
        address.IsActive = true;
        address.IsDeleted = false;
        address.IsmasterAddress = true;
        
        address.IpAdded = HttpContext.Current.Request.UserHostAddress;
    }
    #endregion
    
    #region Bind master data
    private void BindData()
    {
        //#region BindCategory
        //FilterDetail filterDetail = new FilterDetail();
        //filterDetail = new FilterDetail(_dataObject);
        //List<FilterDetail> filterDetails = new List<FilterDetail>();
        //filterDetail.FilterId = (Int32)EnumClass.Enums.Filter.Category;
        //filterDetails = filterDetail.GetAllFilterDetailByFilterId();
        //drpCategory.DataSource = filterDetails;
        //drpCategory.DataBind();
        //drpCategory.Items.Insert(0, new ListItem("Select", "0"));
        //#endregion

        #region BindState
        //Bind State
        State state = new State();
        state = new State(_dataObject);
        List<State> states = new List<State>();
        state.CountryId = (Int32)EnumClass.Enums.Country.India;
        states = state.GetStateByCountryId();
        drpState.DataSource = states;
        drpState.DataBind();
        drpState.Items.Insert(0, new ListItem("Select", "0"));
        drpCity.Items.Insert(0, new ListItem("Select", "0"));
        #endregion
    }
    #endregion

    #region Web Methods
    [System.Web.Services.WebMethod]
    public static List<CityInfo> LoadCity(string stateId)
    {
        IDataRepository _dataObject = new DataRepository();
        City city = new City();
        List<City> cities = new List<City>();
        city = new City(_dataObject);
        city.StateId = Convert.ToInt32(stateId);
        cities = city.GetCityByStateId();
        // return filters;
        List<CityInfo> ci = new List<CityInfo>();
        for (int i = 0; i < cities.Count(); i++)
        {
            ci.Add(new CityInfo()
            {
                Id = Convert.ToInt32(cities.ElementAt(i).Id),
                Name = cities.ElementAt(i).Name
            });
        }
        return ci;
        //return CountryInformation;
    }
    public class CityInfo
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    [System.Web.Services.WebMethod]

    public static bool checkEmailIdAvialibility(string EmailId)
   {
        IDataRepository _dataObject = new DataRepository();
        if (!string.IsNullOrEmpty(EmailId))
        {
            Merchant merchant = new Merchant();
            merchant = new Merchant(_dataObject);
            merchant.Email = EmailId;
            merchant.GetMerchantByEmailId();
            if (merchant.Id > 0)
                return true;
            else
                return false;
        }
        else
            return false;
    }
    #endregion
}